<!DOCTYPE html> 
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta charset="utf-8">
<title>分享页面</title>
<link href="jquery-mobile/jquery.mobile.min.css" rel="stylesheet" type="text/css"/>
<script src="jquery-mobile/jquery.min.js" type="text/javascript"></script>
<script src="jquery-mobile/jquery.mobile.min.js" type="text/javascript"></script>
<script src="js/cordova.js" type="text/javascript"></script>
<!--<script type="text/javascript" src="http://qzonestyle.gtimg.cn/qzone/openapi/qc_loader.js" data-appid="100284268" data-redirecturi="http://ihavecar.sinaapp.com/qc_callback.html" charset="utf-8"></script>
-->
<script type="text/javascript" src="js/jquery.raty.js"></script>

<script type="text/javascript">
$(function() {
	$('#star').raty({
		size:      24,
		starHalf:  'star-half-big.png',
		starOff:   'star-off-big.png',
		starOn:    'star-on-big.png'
	});
});

function back(){
	if (typeof(navigator) != 'undefined' && typeof(navigator.app) != 'undefined' && typeof(navigator.app.backHistory) == 'function') {
		navigator.app.backHistory();
	} else{
		history.back();
	}
}

$(document).ready(function(){
	$("#content").css("min-height", document.body.clientHeight - $("#content").offset().top);
	
	//getGPS();
	jQuery('body').live('swipeleft',function(event){
		back();
		//event.preventDefault();
	});
	jQuery('body').live('swiperight',function(event){
		window.history.forward();
		//event.preventDefault();
	});
});

$('.ui-btn-back').live('tap',function() {
	back();
	return false;
}).live('click',function() {
	back();
	return false;
});
</script>

<style type="text/css">
#sina {
	background: url('img/ucar_icon.gif') no-repeat;
	background-position: 0 0;
	width: 16px;
	height: 16px;
}

</style>
</head>

<body>
<div data-role="page">

 <div data-role="header" data-theme="b">
   <a href="#" class="ui-btn-back">返回</a>
   <h1>点评与分享</h1>
   <a input id="submit" onClick="share()">发送</a>
 </div>
 
 <div data-role="content" id="content">
 <form action="" method="post">	
 	<div style="text-align: center" >
 		 <span  style="border-bottom: solid gray 1px" id="star"></span>
 	</div>
    
  <label for="textarea-a">发表评论: <span id="counter" style="float:right;">140字</span></label>
	<textarea name="textarea" id="textarea-a" autofocus onkeydown='countChar("textarea-a","counter");' onkeyup='countChar("textarea-a","counter");'></textarea>
    
  <div onClick="setshare()"><span style="float:right;">我要分享到&nbsp;&nbsp;<img id="sina"></img> </span>
  
  </div>
 </form>
 </div>


<script type="text/javascript">
//从商户页面获取微博共享所需要的商户信息
var url=window.location.href;
var arg1=url.split("=");
var arg2=arg1[1].split("&");
var type=arg2[0];
var arg3=arg1[2].split("&");
var shopid=arg3[0];
var arg4=arg1[3].split("&");
var shopname=decodeURI(arg4[0]);
var arg5=arg1[4].split("&");
var shoplat=arg5[0];
var shoplng=arg1[5];

//新浪微博选择开关
var IFshare=1;

var star=new Array(" ","★","★★","★★★","★★★★","★★★★★");

//checklogin();
readfile(0);

//可切换是否共享到SINA，默认允许共享
function setshare(){
	if (IFshare==1){
		IFshare=0;
		$("#sina").css("background-position", "0 -16px");
	}else if (IFshare==0){
		IFshare=1;
		$("#sina").css("background-position", "0 0");
	}else{
		console.log("Wrong IFshare status. Check code");
	}
}

function countChar(textareaName,spanName)
{
	document.getElementById(spanName).innerHTML = 140 - document.getElementById(textareaName).value.length;
}


function share() {
	readfile(1);
}

function safecheck( str ){
	str=str.replace(/"/g,"^");  //双引号必须替换，否则出现解析错误
	str=str.replace(/%/g,"_%"); //去除%
	str=str.replace(/</g,"<_"); //去除潜在html标签
	str=str.replace(/>/g,"_>"); //去除潜在html标签
	return str;
}

function readfile(flag) {
	document.addEventListener("deviceready", onDeviceReady, false);
    // PhoneGap is ready
    function onDeviceReady() {
				//checkConnection();
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    }

    function gotFS(fileSystem) {
        fileSystem.root.getFile("ucar/DataFile.txt", {create: true}, gotFileEntry, fail);
    }

    function gotFileEntry(fileEntry) {
        fileEntry.file(gotFile, fail);
    }

    function gotFile(file){
        readAsText(file);
    }

    function readAsText(file) {
				//alert("hello");
        var reader = new FileReader();
        reader.onloadend = function(evt) {
            //console.log("Read as text");
            //console.log(evt.target.result);
			var arg=evt.target.result.split(";");
            if (arg==""){
                // Empty file. Not Logined.
                $("#sina").css("background-position", "0 -16px");
                alert("哎呀，请你先登录，才可以进行点评与分享");
								window.location.href ='login.html';
            }
			if(!flag){
			//在用户输入评论前先检查授权是否过期
				 var arg4=arg[4].split("=");
				 var expire=arg4[1];
				 var d = new Date();
				 var now=d.getTime()/1000;

				if(expire<now){
					alert("哎呀，你的登录授权已经过期，请重新授权~");
					window.location.href ='login.html';
				}
			}else{
				var arg1=arg[0].split("=");
				 var arg2=arg[1].split("=");
				 var uid1=arg1[1];
				 var access_token1=arg2[1];
			
				var score = $('#star').raty('score');
				if (score== ""){
					score=0;
				}
				var con = document.getElementsByName('textarea');
				con = safecheck(con[0].value);
				
				//开始分享，必须等待上一步已经完成才切换到下一步	
				if (IFshare==1){
				$.ajax({type:'POST',async:false, url:'https://api.weibo.com/2/statuses/update.json',data:{access_token:access_token1,uid:uid1,client_id:"3943049038",status:"我在"+shopname+"，"+con+" ，评分："+star[score],lat:shoplat,long:shoplng}}, postserver() );
				}else {
					postserver();
				}
				
				function postserver(){
				$.ajax({type:'POST',async:false, url:'http://ihavecar.sinaapp.com/postcomment.php?shopid='+shopid+'&type='+type+'&userid='+uid1+'&score='+score+'&content="'+con+'"'}, returnshop());
				}
				
				function returnshop(){
				window.location.href ='shopinfo.html?type='+type+'&shopid='+shopid;
				}
			}
        };
        reader.readAsText(file);	
    }

    function fail(evt) {
				 $("#sina").css("background-position", "0 -16px");
                alert("哎呀，请你先登录，才可以进行点评与分享");
								window.location.href ='login.html';
				console.log("Detect as NOT logined.");
        console.log(evt.target.error.code);
    }
}

</script>
</body>
</html>